<?php
/**
 * Created by PhpStorm.
 * User: witt
 * Date: 2017/12/20
 * Time: 16:00
 */
require MX_SHOP_PATH . 'core/web/mroles/page_merch.php';
class Index_MxShopPage extends Page {
    public function main(){

        global $_W, $_GPC;
        if($_W['ispost']){
            //验证令牌
            if(empty($_GPC['token']) || !checkToken($_GPC['token'])){
                show_json(0, '参数有误');
            }
            $ac = $_GPC['account'];
            $pwd = $_GPC['pwd'];

            $result = pdo_fetch('SELECT * FROM '.tablename('mx_shop_role_manage').' WHERE account = :account AND status = 1',
                array(
                    ':account'=>$ac
                )
            );
            if(!empty($result)){
                if(md5(md5($pwd).$result['salt']) == $result['password']){
                    show_json(1, array('message'=>'请求成功'));
                }
            }else{
                show_json(0, array('message'=>'帐号错误'));
            }
        }
        $token = getToken();
        include $this->template('mroles/member/index');
    }
    public function updatepwd(){
        global $_W, $_GPC;
        if($_W['ispost']){
            //验证令牌
            if(empty($_GPC['token']) || !checkToken($_GPC['token'])){
                show_json(0, '参数有误');
            }
            $ac = $_GPC['account'];
            $code = $_GPC['code'];
//            $userid = $_GPC['userid'];//用户ID
            $oldpwd = $_GPC['oldpwd'];//原密码
            $newpwd = $_GPC['newpwd'];//新密码
            $sure_newpwd = $_GPC['sure_newpwd'];//确认新密码

            //检测用户信息是否为空
            if(empty($code)){
                show_json(0, array('message'=>'请输入验证码'));
            }elseif(empty($ac)){
                show_json(0, array('message'=>'用户无法认证'));
            }elseif (empty($oldpwd)){
                show_json(0, array('message'=>'请输入原密码'));
            }elseif (empty($newpwd)){
                show_json(0, array('message'=>'请输入新密码'));
            }elseif (empty($sure_newpwd)){
                show_json(0, array('message'=>'请输入确认密码'));
            }

            //检测新密码与确认新密码是否相同
            if($newpwd != $sure_newpwd){
                show_json(0, array('message'=>'两次输入密码不相同'));
            }
            //验证旧密码
            $result = pdo_fetch('SELECT * FROM '.tablename('mx_shop_role_manage').' WHERE account = :account AND status = 1',
                array(
                    ':account'=>$ac
                )
            );
            if(!empty($result)){
                if(md5(md5($oldpwd).$result['salt']) == $result['password']){
                    $jg = true;
                }else{
                    $jg = false;
//                    show_json(0, array('message'=>'原密码错误'));
                }
            }else{
                show_json(0, array('message'=>'未知错误，用户不存在？'));
            }

            //修改密码
            if($jg==true){
                $newpwd = md5(md5($newpwd).$result['salt']);
                $u_result = pdo_update('mx_shop_role_manage',array('password'=>$newpwd),array('account'=>$ac));
                if($u_result){
                    show_json(1, array('message'=>'请求成功'));
                }
            }else{
                show_json(0, array('message'=>'原密码错误'));
            }



        }
        $token = getToken();
        echo $token;die;
        include $this->template('mroles/member/change_pwd');
    }

}